package com.itqf.controller;

import com.itqf.pojo.Goods;
import com.itqf.service.GoodsService;
import com.itqf.service.impl.GoodsServiceImpl;
import com.itqf.utils.Constants;
import com.itqf.vo.PageBean;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.Map;

/**
 * projectName: gpshop
 *
 * @author: 赵伟风
 * time: 2021/7/22 16:08
 * description:获取类型的Controller
 */
@WebServlet("/goods/*")
public class GoodsController extends BaseServlet {


    /**
     *
     * 分页商品!
     *
     *    1.前端参数: page = 1 pageSize = 2 如果没有传,自己设定默认值!
     *              cId 类别id 如果没有传,查全部数据! [搜索关键字查询!]
     *
     *    2.后台返回:PageBean
     * @param request
     * @param response
     * @return
     */
    public String show(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //1.获取所有请求参数
        Map<String, String[]> parameterMap = request.getParameterMap();

        //2.调用业务查询pageBean
        GoodsService goodsService = new GoodsServiceImpl();
        PageBean<Goods> pageBean = goodsService.findPage(parameterMap);

        //3.响应
        request.setAttribute("pageBean", pageBean);

        return Constants.FORWARD_ACTION + "/goodsList.jsp";
    }


    /**
     * 查看商品详情接口
     * @param request
     * @param response
     * @return
     */
    public String detail(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //1.获取请求参数
        String gId = request.getParameter("gId");

        //2.查询商品详情
        GoodsService goodsService = new GoodsServiceImpl();
        Goods goods = goodsService.findDetail(gId);

        //3.转发响应
        request.setAttribute("goods", goods);
        return Constants.FORWARD_ACTION + "/goodsDetail.jsp";
    }


}
